/** <a href="http://www.cpupk.com/decompiler">Eclipse Class Decompiler</a> plugin, Copyright (c) 2017 Chen Chao. **/
package com.minierp.dao.hibernate;

import com.minierp.dao.DepartmentDao;
import com.minierp.dao.hibernate.BaseDaoImpl;
import com.minierp.model.Department;
import java.sql.SQLException;
import java.util.List;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.springframework.orm.hibernate3.HibernateCallback;

public class DepartmentDaoImpl extends BaseDaoImpl implements DepartmentDao {
	public List getList(boolean justEnabled) {
		String sql = "from com.minierp.model.Department c";
		if (justEnabled) {
			sql = sql + " where c.enabled=\'Y\'";
		}

		sql = sql + " order by c.parentId,c.id";
		return this.getHibernateTemplate().find(sql);
	}

	public List getAllChildren(long parentId) {
		String sql = "from com.minierp.model.Department as obj where obj.parentIdStr like :parentIdStr order by obj.id";
		String parentIdStr = "%;" + parentId + ";%";
		String[] paramNames = new String[]{"parentIdStr"};
		Object[] values = new Object[]{parentIdStr};
		return this.getHibernateTemplate().findByNamedParam(sql, paramNames,
				values);
	}

	public int unenabledTree(final long id) {
		Object obj = this.getHibernateTemplate().execute(
				new HibernateCallback() {
					public Object doInHibernate(Session session)
							throws HibernateException, SQLException {
						String hql = "update com.minierp.model.Department set enabled=\'N\' where id=:id or parentIdStr like :parentIdStr";
						String parentIdStr = "%;" + id + ";%";
						return new Integer(session.createQuery(hql)
								.setLong("id", id)
								.setString("parentIdStr", parentIdStr)
								.executeUpdate());
					}
				});
		return ((Integer) obj).intValue();
	}

	public Department get(String departmentCode) {
		String sql = "from com.minierp.model.Department as obj where obj.enabled=\'Y\' and obj.departmentCode = :departmentCode order by obj.id";
		String[] paramNames = new String[]{"departmentCode"};
		Object[] values = new Object[]{departmentCode};
		List list = this.getHibernateTemplate().findByNamedParam(sql,
				paramNames, values);
		return list.isEmpty() ? null : (Department) list.get(0);
	}
}